#include <stdio.h>
#include <string.h>

char  str[4096];
int StringBracket(char *str)//删除空格和制表符
{
	int i,j,len;

	len=strlen(str);
	j=0;
	for(i=0; i<len; i++)
		{
			if(str[i]!=' ' && str[i]!='\t')
				{
					str[j]=str[i];
					j=j+1;
				}
		}
	str[j]='\0';
	if(str[0]=='\n') return 0;
	else return j;
}

void   main()
{
	int    all=0,zs=0,kg=0,j,k=0;
	FILE   *fp;
	char   filename[100];

	printf("\nEnter a filepath/filename:\n");
	gets(filename);
	fp=fopen(filename, "rt");
	if(fp==NULL)
		{
			printf("cannot open file\n");
			return;
		}
	while (fgets(str,4096,fp))  //读取一行，并判断文件是否结束
		{
			j=StringBracket(str);
			if(k==0)
				{
					if(j==0) kg++;
					else if(str[0]=='/')
						{
							if(strstr(str,"/*")) k=1;
							if(strstr(str, "*/"))k=0;
							zs++;
						}
				}
			else
				{
					if(strstr(str, "*/"))k=0;
					zs++;
				}
			all++;
		}
	fclose(fp);
	printf("filename:%s 代码行数:%d 注释行:%d 空行:%d",filename,all-zs-kg,zs,kg);
}
